<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Models\Vip
 *
 * @property int $id
 * @property int $uid uid
 * @property string $vip_card_sn 会员卡卡号
 * @property string $shop_name 所属店铺
 * @property int $vip_card_status 会员卡状态 0未绑定 1已绑定 2 已禁用
 * @property \Carbon\Carbon $created_at 创建时间
 * @property \Carbon\Carbon $updated_at 修改时间
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereShopName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereUid($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereVipCardSn($value)
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Vip whereVipCardStatus($value)
 * @mixin \Eloquent
 */
class Vip extends Model
{
    public $table = 'vip';
    public static function getVipCardList()
    {
        $data = self::leftJoin('user','user.id','=','vip.uid')
                    ->select('user.user_name','user.user_nickname','vip.*')
                    ->get();
        if (empty($data)){
            return [];
        }else{
            return $data->toArray();
        }
    }


    public static function getVipCardById($id)
    {
        $data = self::find($id);
        if (empty($data)){
            return [];
        }else{
            return $data->toArray();
        }
    }


    public static function updateById(int $id,array $update)
    {

        $flag = self::where('id',$id)->update($update);
        return $flag;
    }


    public static function  addVipCared($data)
    {
        $flag = self::insertGetId($data);
        return $flag;
    }

    public static function isBind($vip_card_sn)
    {
        $data = self::where('vip_card_sn',$vip_card_sn)->where('vip_card_status',0)->first();

        if (!empty($data)){
            $data = $data->toArray();
        }else{
            $data = [];
        }

        return $data;
    }


    public static function bindVipCard($vip_card_sn,$data)
    {
        $flag = self::where('vip_card_sn',$vip_card_sn)->update($data);
        return $flag;
    }


    public static function  getInfoByVipSn($vip_cared_sn)
    {
        $data = self::where('vip_card_sn',$vip_cared_sn)->where('vip_card_status',1)->first();
        if (empty($data)){
            return [];
        }

        return $data->toArray();
    }




}
